<?php

include("aids.class.php");
include("drugdata.class.php");

class BAL
{
    public $service;

    function  __construct()
    {
        $this->service = new AIDS();
    }

    public function getSearchKeyword($queryString)
    {
        $searchKeyword = "";

        $this->params = $this->getQueryStringValues($queryString);

        if($this->params)
        {
            if(isset ($this->params["searchValue"]))
            {
                if(is_array($this->params["searchValue"]) && count($this->params["searchValue"]) > 0)
                {
                    $searchKeyword = $this->params["searchValue"][0];
                }
                else
                {
                    $searchKeyword = $this->params["searchValue"];
                }
            }
        }

        return $searchKeyword;
    }

    public function getSearchResults($keyword)
    {
        $results_array = array();

        $drug = $this->service->DrugSearch($keyword);
        
        if($drug != "Drug does not exist")
        {
            $drug_data = new DrugData();

            $pharm = $drug->manufacturers->pharm[0];
            $summary = $drug->summaries->summary;

            $drug_data->drugName = $pharm->pharmDrug;
            $drug_data->company = $pharm->company;
            $drug_data->description = $summary->description;


            foreach($drug->names->name as $drug_name)
            {
                if($drug_name["type"] == "Generic")
                {
                    $drug_data->id = $drug_name;
                }
                else if($drug_name["type"] == "AKA" && empty($drug_data->aka))
                {
                    $drug_data->aka = $drug_name;
                }
            }

            $drug_data->url = $this->service->searchHost . "?name=" . urlencode($keyword);

            $results_array[] = $drug_data;
        }
        
        return $results_array;
    }


    public function getTotalRecords()
    {
        return $this->totalRecords;
    }

    public function getQueryStringValues($query_string)
    {
        $query  = explode('&', $query_string);
        $params = array();

        foreach( $query as $param )
        {
            list($name, $value) = explode('=', $param);
            $params[urldecode($name)][] = urldecode($value);
        }

        return $params;
    }

    public function getSearchCount()
    {
        $searchCount = 10;

        if($this->params)
        {
            if(isset ($this->params["numberOfRows"]))
            {
                if(is_array($this->params["numberOfRows"]) && count($this->params["numberOfRows"]) > 0)
                {
                    $searchCount = $this->params["numberOfRows"][0];
                }
                else
                {
                    $searchCount = $this->params["numberOfRows"];
                }
            }
        }

        return $searchCount;
    }

    public function getStartRecord()
    {
        $startRecord = 1;

        if($this->params)
        {
            if(isset ($this->params["startRow"]))
            {
                if(is_array($this->params["startRow"]) && count($this->params["startRow"]) > 0)
                {
                    $startRecord = $this->params["startRow"][0];
                }
                else
                {
                    $startRecord = $this->params["startRow"];
                }
            }
        }

        return $startRecord;
    }
}
?>
